package com.oocl.training.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.TreeSet;

import com.oocl.training.clean.impl.CleanCustomer;
import com.oocl.training.pojo.Customer;
import com.oocl.training.sort.BirthdayComparator;
import com.oocl.training.sort.IdComparator;
import com.oocl.training.util.PrintUtil;

/**
 * 主入口
 * @author GANAB
 *
 */
public class Main {
	//清洗数据
	private CleanCustomer cc = new CleanCustomer();
	//用户数组
	private Set<Customer> customerList = new TreeSet<Customer>();

	/**
	 * Main入口
	 * @param args
	 */
	public static void main(String[] args) {
		Main main = new Main();
		System.out.println("------ID排序并输出-------");
		main.sortByIdAndPrint();
		System.out.println("------Birthday排序并输出-------");
		main.sortByBirthdayAndPrint();
		System.out.println("------XML格式排序并输出-------");
		main.printByXml();
	}
	
	public Main() {
		init();
	}
	
	private void init(){
		System.out.println("正在读取data.txt数据……");
		BufferedReader br;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("data.txt"))));
			String str = "";
			while((str=br.readLine())!=null){
				Customer customer = cc.clean(str);
				if(customer!=null){
					customerList.add(customer);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 按照ID排序并输出
	 */
	public void sortByIdAndPrint(){
		Set<Customer> newSet = new TreeSet<Customer>(new IdComparator());
		for(Customer c : customerList){
			newSet.add(c);
		}
		PrintUtil.simplePrint(newSet);
	}
	
	/**
	 * 按照生日排序并输出
	 */
	public void sortByBirthdayAndPrint(){
		Set<Customer> newSet = new TreeSet<Customer>(new BirthdayComparator());
		for(Customer c : customerList){
			newSet.add(c);
		}
		PrintUtil.simplePrint(newSet);
	}
	
	/**
	 * XML格式输出
	 */
	public void printByXml(){
		PrintUtil.xmlPrint(customerList);
	}

	
}
